package com.gexiaobao.pigeon.ui.fragment.mall

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.ModifyAddressParam
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.FragmentModifyOrderBinding
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import com.lljjcoder.Interface.OnCityItemClickListener
import com.lljjcoder.bean.CityBean
import com.lljjcoder.bean.DistrictBean
import com.lljjcoder.bean.ProvinceBean
import com.lljjcoder.citywheel.CityConfig
import com.lljjcoder.style.citypickerview.CityPickerView
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/7/6 18:05
 * @Describe    : 修改订单
 */
class FragmentModifyOrder : BaseFragment<MallViewModel, FragmentModifyOrderBinding>() {

    private val mPicker = CityPickerView()
    private var mOrderId = 0

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = "修改订单"
        mOrderId = requireArguments().getInt("orderId")
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.modifyAddressResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                RxToast.showToast("操作成功")
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.tvOrderReceiveAddress,
            mDatabind.tvModifyAddress,
            mDatabind.llOrderModifyAddress,
            mDatabind.includeBar.ivBack
        ) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.llOrderModifyAddress, mDatabind.tvOrderReceiveAddress -> {
                    chooseCityData()
                }

                mDatabind.tvModifyAddress -> {
                    val orderReceiveName = mDatabind.etOrderReceiveName.text.toString().trim()
                    val orderPhone = mDatabind.etOrderReceivePhone.text.toString().trim()
                    val orderAddress =
                        mDatabind.tvOrderReceiveAddress.text.toString().trim() + mDatabind.etOrderReceiveAddressDetail.text.toString().trim()
                    when {
                        orderReceiveName.isEmpty() -> showMessage("填写联系人姓名")
                        orderPhone.isEmpty() -> showMessage("填写联系人电话")
                        else -> {
                            showLoading(getString(R.string.loading))
                            val body = ModifyAddressParam(
                                orderId = mOrderId,
                                orderReceiver = orderReceiveName,
                                orderContactPhone = orderPhone,
                                orderAddress = orderAddress
                            )
                            mViewModel.editOrderReceiveAddress(body)
                        }
                    }
                }
            }
        }
    }

    private fun chooseCityData() {
        hideSoftKeyboard(activity)
        val cityConfig = CityConfig.Builder().build()
        cityConfig.title = "选择地区"
        cityConfig.titleTextSize = 20
        cityConfig.confirmTextSize = 18
        cityConfig.cancelTextSize = 18
        cityConfig.titleTextSize = 18
        cityConfig.visibleItems = 8
        cityConfig.defaultProvinceName = "山东省"//默认显示的省份
        cityConfig.defaultCityName = "青岛市"
        cityConfig.customItemLayout = R.layout.item_city
        cityConfig.customItemTextViewId = R.id.item_city_name_tv
        mPicker.init(context)
        mPicker.setConfig(cityConfig)

        mPicker.setOnCityItemClickListener(object : OnCityItemClickListener() {
            @SuppressLint("SetTextI18n")
            override fun onSelected(province: ProvinceBean?, city: CityBean?, district: DistrictBean?) {
                super.onSelected(province, city, district)
                mDatabind.tvOrderReceiveAddress.text = province!!.name + "-" + city!!.name + "-" + district!!.name
            }
        })
        mPicker.showCityPicker()
    }
}